﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Vehicle_Maintenance
{
    class Person
    {
        public Person() { }
        public string ID           = string.Empty;
        public string FirstName    = string.Empty;
        public string LastName     = string.Empty;
        public string LicenseState = string.Empty;
        public string LicenseNo    = string.Empty;
        public string Insurrance   = string.Empty;

        public bool Load()
        {
            bool b_isLoaded = false;
            if ( !string.IsNullOrWhiteSpace( this.ID ) )
            {
                DataTable dt_person = new DataTable();
                using ( SqlConnection scConnect = new SqlConnection( Properties.Settings.Default.Auto_Log ) )
                {
                    scConnect.Open();
                    SqlCommand scCommand = new SqlCommand();
                    scCommand.Connection = scConnect;
                    scCommand.CommandType = CommandType.Text;
                    scCommand.CommandText = "SELECT * FROM dbo.Users WHERE ID = '" + this.ID + "'";
                    dt_person.Load( scCommand.ExecuteReader() );
                    scConnect.Close();
                }
                using ( DataTableReader dtr = new DataTableReader( dt_person ) )
                {
                    if ( dtr.HasRows )
                    {
                        dtr.Read();
                        FirstName    = dtr[ "F_Name" ].ToString();
                        LastName     = dtr[ "L_Name" ].ToString();
                        LicenseState = dtr[ "Lic_State" ].ToString();
                        LicenseNo    = dtr[ "License" ].ToString();
                        Insurrance   = dtr[ "Insur" ].ToString();
                        b_isLoaded   = true;
                    }
                }
            }
            return b_isLoaded;
        }
        public bool LoadByID(string id)
        {
            ID = id;
            return Load();
        }

        public void New()
        {
            if ( !string.IsNullOrWhiteSpace( this.FirstName ) &&
                !string.IsNullOrWhiteSpace( this.LastName ) &&
                !string.IsNullOrWhiteSpace( this.LicenseState ) &&
                !string.IsNullOrWhiteSpace( this.LicenseNo ) )
            {
                using ( SqlConnection scConnect = new SqlConnection( Properties.Settings.Default.Auto_Log ) )
                {
                    scConnect.Open();
                    SqlCommand scCommand = new SqlCommand();
                    scCommand.Connection = scConnect;
                    scCommand.CommandType = CommandType.StoredProcedure;
                    scCommand.CommandText = "users_insert";
                    scCommand.Parameters.AddWithValue( "@FirstName", this.FirstName );
                    scCommand.Parameters.AddWithValue( "@LastName", this.LastName );
                    scCommand.Parameters.AddWithValue( "@License", this.LicenseNo );
                    scCommand.Parameters.AddWithValue( "@LicenseState", this.LicenseState );
                    scCommand.Parameters.AddWithValue( "@Insurrance", this.Insurrance );
                    scCommand.Parameters.AddWithValue( "@ID", DBNull.Value );
                    scCommand.Parameters[ "@ID" ].Direction = ParameterDirection.Output;
                    scCommand.Parameters[ "@ID" ].Size = 16;
                    scCommand.ExecuteNonQuery();
                    this.ID = scCommand.Parameters[ "@ID" ].Value.ToString();
                    scConnect.Close();
                }
            }
        }
    }
}
